package com.example.clientend.config;

import com.example.clientend.utils.SLWebSocketHandler;
import org.springframework.context.annotation.Configuration;

import javax.websocket.*;

// WebSocket客户端配置
@ClientEndpoint
@Configuration
public class WebSocketConfig {

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("连接到WebSocket服务器");
    }

    @OnMessage
    public void onMessage(String message) {
        // 将服务器返回的数据保存在静态变量中，供SLWebSocketHandler类使用。receivedMessage非空时就说明收到了服务器返回的数据。
        SLWebSocketHandler.receivedMessage = message;
    }

    @OnClose
    public void onClose() {
        System.out.println("WebSocket连接关闭");
    }

    @OnError
    public void onError(Throwable throwable) {
        System.err.println("WebSocket连接错误: " + throwable.getMessage());
    }
}
